package mchecca.model;

import java.util.ArrayList;

/**
 * Class for a line in a DLE.
 * <p>
 * A {@link Line} has the following properties
 * <ul>
 * 	<li>String representation of a choice</li>
 * 	<li>Position amongst other {@link Line}s</li>
 * 	<li>List of {@link Edge}s</li>
 * </ul>
 * @author Michael Checca
 */
public class Line {
	/** Name of the choice */
	String choice;
	/** Position of the line */
	int position;
	/** List of edges connecting this line to others */
	ArrayList<Edge> edges;

	/**
	 * Create a new line.
	 * <p>
	 * The new {@link Line} has a default choice of the empty string "" and no edges
	 * @param position Position of the line
	 */
	public Line(int position) {
		this.position = position;
		this.choice = "";
		this.edges = new ArrayList<Edge>();
	}

	/**
	 * Returns the next edge by height.
	 * @param n Pixel height to get edges after
	 * @return Next {@link Edge} with a height greater than n
	 */
	public Edge nextEdge(int n) {
		Edge next = null;
		for (Edge e: edges)
			if (e.height > n) {
				if (next == null)
					next = e;
				else if (e.height < next.height && e.height > n)
					next = e;
			}
		return next;
	}

	/**
	 * Gets the list of edges.
	 * @return List of {@link Edge}s
	 */
	public ArrayList<Edge> getEdges() {
		return edges;
	}

	/**
	 * Determines if two {@link Line}s are equal
	 * <p>
	 * Two {@link Line}s are equal if the have the same
	 * <ul>
	 * 	<li>Position</li>
	 * 	<li>Choice</li>
	 * 	<li>List of {@link Edge}s</li>
	 * </ul>
	 * @param obj Other object to compare to this {@link Line}
	 * @return True if obj is the same is this object, false otherwise
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Line) {
			Line other = (Line) obj;
			return position == other.position &&
					choice.equals(other.choice) &&
					edges.equals(other.edges);
		}
		return false;
	}

	/**
	 * Formats this {@link Line} nicely into a string.
	 * @return String representation of this {@link Line}
	 */
	@Override
	public String toString() {
		return String.format("Line %d [%s]", position, choice);
	}

	/** Gets the line's choice */
	public String getChoice() {
		return choice;
	}

	/** Sets the line's choice */
	public void setChoice(String choice) {
		this.choice = choice;
	}
}
